/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package GUI;
import JMS.JmsConsumerP2P;
import JMS.JmsConsumerP2P.Types;
import JMS.JmsProducerP2P;
import Others.Plateau;
import Pions.Cavalier;
import Pions.Fou;
import Pions.Piece;
import Pions.Piece.Couleur;
import Pions.Reine;
import Pions.Roi;
import Pions.Pion;
import Pions.Tour;
import ant.Pions;
import ejb.SessionBeanRemote;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.Border;
import sun.nio.cs.MS1250;
/**
*
* @author Greenlamp
*/
public class Main extends javax.swing.JFrame implements MessageListener{
SessionBeanRemote sessionBean;
/**
* Creates new form Main
*/
Plateau[][] plateau;
JPanel chessboard;
String nomPlateau = null;
List<Point> deplacementsPossible;
Plateau selected;
Couleur couleurJoueur;
Lobby parent;
boolean waitAdversaire;
String idMessage;
String idMessageAdverse;
JmsProducerP2P producer;
JmsConsumerP2P consumer;
boolean temp;
public enum MSG{
NOUVEAUJOUEUR,
JOUEURPARTI,
DEPLACEMENT,
ECHEC,
ECHECETMAT;
public String getValue(){
return this.name().toLowerCase();
}
};
public Main() {
initBoard();
}
Main(SessionBeanRemote sessionBean, String nomPlateau, int couleur, Lobby parent) {
this.setSize(480, 480);
setLocationRelativeTo(null);
this.nomPlateau = nomPlateau;
this.sessionBean = sessionBean;
this.selected = null;
this.parent = parent;
this.setTitle("Plateau: " + this.nomPlateau +" Couleur: " + (couleur == 0 ? "Blanc" : "Noir"));
couleurJoueur = (couleur == 0 ? Couleur.BLANC : Couleur.NOIR);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e){
quitterPartie();
}
});
int idJoueur = sessionBean.getIdJoueur(this.nomPlateau, couleur);
this.idMessage = nomPlateau + "-" + "j:" + idJoueur;
producer = new JmsProducerP2P();
consumer = new JmsConsumerP2P(Types.ASYNC, this.idMessage);
consumer.addListener(this);
initJoueurs();
initBoard();
refreshPlateau();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
setBounds(0, 0, 416, 338);
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Main.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Main().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
// End of variables declaration//GEN-END:variables
private void initBoard() {
plateau = new Plateau[8][8];
chessboard = new JPanel(new GridLayout(8, 8));
Border selected = null;
selected = BorderFactory.createLineBorder(Color.BLACK);
for(int x=0; x<8; x++){
for(int y=0; y<8; y++){
if((x + y) % 2 == 0){
plateau[x][y] = new Plateau(this, x, y, Color.GRAY);
}else{
plateau[x][y] = new Plateau(this, x, y, Color.WHITE);
}
plateau[x][y].setBorder(selected);
chessboard.add(plateau[x][y]);
}
}
chessboard.updateUI();
this.add(chessboard);
}
private void clearPlateau() {
for(int x=0; x<8; x++){
for(int y=0; y<8; y++){
plateau[x][y].removePion();
}
}
chessboard.updateUI();
}
private void refreshPlateau() {
List<Pions> pions = sessionBean.findByNomPlateau(nomPlateau);
List<Piece> listePion = new ArrayList<Piece>();
for(Pions pion : pions){
if(pion.getNomPion().equalsIgnoreCase("Tour")){
listePion.add(new Tour(pion.getPositionX(), pion.getPositionY(), pion.getCouleur()));
}else if(pion.getNomPion().equalsIgnoreCase("Cavalier")){
listePion.add(new Cavalier(pion.getPositionX(), pion.getPositionY(), pion.getCouleur()));
}else if(pion.getNomPion().equalsIgnoreCase("Fou")){
listePion.add(new Fou(pion.getPositionX(), pion.getPositionY(), pion.getCouleur()));
}else if(pion.getNomPion().equalsIgnoreCase("Reine")){
listePion.add(new Reine(pion.getPositionX(), pion.getPositionY(), pion.getCouleur()));
}else if(pion.getNomPion().equalsIgnoreCase("Roi")){
listePion.add(new Roi(pion.getPositionX(), pion.getPositionY(), pion.getCouleur()));
}else if(pion.getNomPion().equalsIgnoreCase("Pion")){
listePion.add(new Pion(pion.getPositionX(), pion.getPositionY(), pion.getCouleur()));
}
}
for(Piece pion : listePion){
plateau[pion.getX()][pion.getY()].addPion(pion);
}
chessboard.updateUI();
}
private void quitterPartie(){
sessionBean.quitterPartie(nomPlateau, (couleurJoueur == Couleur.BLANC ? 0 : 1));
this.producer.sendMessage(MSG.JOUEURPARTI.getValue(), idMessageAdverse);
this.dispose();
System.exit(0);
}
public void onMouseClick(int x, int y) {
Piece pion = plateau[x][y].getPion();
boolean rouge = plateau[x][y].isSelectionne();
/*if(waitAdversaire){
JOptionPane.showMessageDialog(this, "En attente d'un adversaire");
return;
}*/
if(selected == null){
//deplacementsPossible = pion.getDeplacementPossible(this.plateau);
deplacementsPossible = sessionBean.getDeplacementPossible(pion.getX(), pion.getY(), (pion.getCouleur() == Couleur.BLANC ? 0 : 1), pion.getNom());
for(Point point : deplacementsPossible){
plateau[point.x][point.y].selectionneCase(false);
}
selected = plateau[x][y];
}else{
if(rouge){
for(Point point : deplacementsPossible){
plateau[point.x][point.y].selectionneCase(true);
}
bouger(plateau[x][y]);
selected = null;
}else{
for(Point point : deplacementsPossible){
plateau[point.x][point.y].selectionneCase(true);
}
selected = null;
}
}
}
private void bouger(Plateau caseClicked) {
Piece pion = selected.getPion();
boolean autoriser = sessionBean.bougerPion(pion.getNom(), (pion.getCouleur() == Piece.Couleur.BLANC ? 0 : 1), nomPlateau, selected.getPositionX(), selected.getPositionY(), caseClicked.getPositionX(), caseClicked.getPositionY());
if(autoriser){
clearPlateau();
refreshPlateau();
producer.sendMessage(idMessageAdverse, MSG.DEPLACEMENT.getValue());
}
}
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage)message;
String msg = null;
try {
msg = textMessage.getText();
System.out.println("Message reçu: " + msg);
} catch (JMSException ex) {
Logger.getLogger(Lobby.class.getName()).log(Level.SEVERE, null, ex);
}
if(msg.equalsIgnoreCase(MSG.NOUVEAUJOUEUR.getValue())){
if(waitAdversaire == true){
int idAdversaire = sessionBean.getIdJoueur(nomPlateau, 1);
this.idMessageAdverse = nomPlateau + "-" + "j:" + idAdversaire;
waitAdversaire = false;
}
}else if(msg.equalsIgnoreCase(MSG.JOUEURPARTI.getValue())){
if(!waitAdversaire){
JOptionPane.showMessageDialog(this, "L'adversaire est parti, vous êtes donc vainqueur par forfait !");
sessionBean.quitterPartie(nomPlateau, (couleurJoueur == Couleur.BLANC ? 0 : 1));
sessionBean.deletePlateau(nomPlateau);
parent.dispose();
this.dispose();
System.exit(0);
}
}else if(msg.equalsIgnoreCase(MSG.DEPLACEMENT.getValue())){
clearPlateau();
refreshPlateau();
}else if(msg.equalsIgnoreCase(MSG.ECHEC.getValue())){
refreshPlateau();
}else if(msg.equalsIgnoreCase(MSG.ECHECETMAT.getValue())){
refreshPlateau();
}
}
private void initJoueurs() {
if(couleurJoueur == Couleur.NOIR){
int idAdversaire = sessionBean.getIdJoueur(nomPlateau, 0);
this.idMessageAdverse = nomPlateau + "-" + "j:" + idAdversaire;
this.producer.sendMessage(MSG.NOUVEAUJOUEUR.getValue(), this.idMessageAdverse);
waitAdversaire = false;
}else{
waitAdversaire = true;
}
}
}